package server

import "go.uber.org/zap"

// Server ...
type Server struct {
	etcd    *Etcd
	echo    *Echo
	sql     *MySQL
	agents  *AgentStore
	grafana *Grafana
}

var gServer *Server

// New ...
func New() *Server {
	return &Server{
		etcd:    NewEtcd(Conf.EtcdC.Addrs, Conf.EtcdC.Dltimeout, Conf.EtcdC.ReportTime, Conf.EtcdC.TTL, Conf.EtcdC.ReportDir, Conf.EchoC.Addr),
		echo:    NewEcho(),
		sql:     NewMySQL(Conf.SQLC.Acc, Conf.SQLC.Pw, Conf.SQLC.Addr, Conf.SQLC.Port, Conf.SQLC.Database),
		agents:  NewAgentStore(),
		grafana: NewGrafana(),
	}
}

// Start start
func (server *Server) Start() error {
	gServer = server
	Logger.Info("Server Start")

	server.etcd.Start()
	server.sql.Start()
	if err := server.grafana.Start(); err != nil {
		Logger.Fatal("Start", zap.Error(err))
	}
	go server.echo.Start()
	return nil
}

// Close ...
func (server *Server) Close() error {
	Logger.Info("Server Close")
	return nil
}
